package com.huifaapp.mall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.base.activity.BaseActivity;
import com.huifaapp.mall.base.basemvp.IPresenter;
import com.huifaapp.mall.bean.LoginBean;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

/**
 * Created by Administrator on 2017-11-28.
 */

public class ForgotPasswordNextActivity extends BaseActivity{
    private ImageView iv_forgot_password_next_back;
    private EditText et_forgot_pwd_newpass;
    private EditText et_forgot_pwd_truenewpass;
    private TextView tv_login_forgotPassword_next_true;

    @Override
    protected void initData(Bundle extras) {
        iv_forgot_password_next_back= (ImageView) findViewById(R.id.iv_forgot_password_next_back);
        iv_forgot_password_next_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        et_forgot_pwd_newpass= (EditText) findViewById(R.id.et_forgot_pwd_newpass);
        et_forgot_pwd_truenewpass= (EditText) findViewById(R.id.et_forgot_pwd_truenewpass);
        tv_login_forgotPassword_next_true= (TextView) findViewById(R.id.tv_login_forgotPassword_next_true);
        tv_login_forgotPassword_next_true.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoadLogin();
            }
        });


    }
    private void LoadLogin(){
        String token=getIntent().getStringExtra("token");
        String newpwd=et_forgot_pwd_newpass.getText().toString();
        String trueNewpwd=et_forgot_pwd_truenewpass.getText().toString();
        if(newpwd.equals("")){
            Toast.makeText(ForgotPasswordNextActivity.this,"密码不能为空！",Toast.LENGTH_SHORT).show();
        }else if(trueNewpwd.equals("")){
            Toast.makeText(ForgotPasswordNextActivity.this,"确认密码不能为空！",Toast.LENGTH_SHORT).show();
        }
        Log.e("重置","WYBS"+ AUniqueIdentifier.getDeviceId(ForgotPasswordNextActivity.this));
        String url = ApiManager.USERS_CHGPWD+"?token="+token+"&newpwd="+newpwd+"&confirmpwd="+trueNewpwd+"&appId="+ AUniqueIdentifier.getDeviceId(ForgotPasswordNextActivity.this);
        Log.e("重置","登录url===="+url);
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Gson gson = new Gson();
                LoginBean list = gson.fromJson(responseInfo.result, LoginBean.class);
                if(list.getCode().equals("10000")) {

                    Toast.makeText(ForgotPasswordNextActivity.this, "修改密码成功!", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(ForgotPasswordNextActivity.this,LoginActivity.class);
                    startActivity(intent);
                    finish();
                }else {
                    Toast.makeText(ForgotPasswordNextActivity.this, list.getMsg(), Toast.LENGTH_SHORT).show();
                }
            }


            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(ForgotPasswordNextActivity.this,"失败",Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Override
    protected IPresenter onLoadPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.login_forgot_password_next;
    }
}
